﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;
using Cofe.Core;
using Cofe.Core.Entry;
using Cofe.Core.Implements;
using Cofe.Core.Property;
using Cofe.Core.Security;
using Cofe.Web.Models;
using Cofe.Web.Utils;

namespace Cofe.Web.Controllers
{
    [Authorize(Roles = "Admin")] 
    public class VolumeController : Controller
    {
        //
        // GET: /Volume/
        public ActionResult Index()
        {
            var volumes = VolumeFE.Volumes.Values
                .Select(v => VolumeInfoViewModel.FromVolumeInfo((VolumeInfo)v))
                .ToList();
            return View(volumes);
        }

        //
        // GET: /Volume/Create
        public ActionResult Create()
        {
            ViewBag.VolumeFactories =
                new SelectList(CofeServices.VolumeRegistrar.VolumeFactories
                    .Where(vf => !(vf is EntryTypeVolumeFactory) && !(vf is EntryListVolumeFactory))
                    .ToList().OrderBy(vf => vf.FactoryId),
                    "FactoryId", "FactoryId");
            ViewBag.PermissionTypes = new SelectList(from PermissionType pt in Enum.GetValues(typeof(PermissionType))
                                                     select new { Id = (int)pt, Name = pt.ToString() }, "Id", "Name");
            return View(new VolumeInfoViewModel());
        }
    
        //
        // POST: /Volume/Create
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> Create(VolumeInfoViewModel vivm)
        {
            Func<VolumePermission, PermissionType> upm = (volPerm) => (PermissionType)(int)volPerm; //UpdatePermission

            VolumeFE.MapAsync(upm(vivm.PublicPermission), vivm.VolumeInfo.FactoryId,
                vivm.VolumeInfo.VolumeId,
                vivm.VolumeInfo.ParamString).Wait();

            if (vivm.PublicPermission != VolumePermission.ReadWrite)
            {
                VolumeFE.GrantPermission(new string[] { WebUserRole.Me.GetRoleString() }, vivm.VolumeInfo.VolumeId, PermissionType.All);
                VolumeFE.GrantPermission(new string[] { WebUserRole.FamilyMember.GetRoleString() }, vivm.VolumeInfo.VolumeId, upm(vivm.FamilyMemberPermission));
                VolumeFE.GrantPermission(new string[] { WebUserRole.Friend.GetRoleString() }, vivm.VolumeInfo.VolumeId, upm(vivm.FriendPermission));                
            }

            await CofeServices.SaveAsync();
            return RedirectToAction("Index");
        }


        //
        // GET: /Volume/Delete/5
        public ActionResult Delete(string volumeId = null)
        {
            if (VolumeFE.Volumes.ContainsKey(volumeId))                
                return PartialView(VolumeInfoViewModel.FromVolumeInfo(VolumeFE.Volumes[volumeId]));
            else return HttpNotFound();
        }

        //
        // POST: /Volume/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> DeleteConfirmed(string volumeId = null)
        {
            if (VolumeFE.Volumes.ContainsKey(volumeId))
                VolumeFE.Unmap(volumeId);
            await CofeServices.SaveAsync();
            return RedirectToAction("Index");
        }
    }
}
